<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyAncestor extends BaseModel
{
    protected $name = 'sky_ancestor';


    /**
     * 先祖分类
     * @param $category
     * @return mixed
     */
    public function tag($category){
        $list = $this->field('type')
            ->where('category','=',$category)
            ->group('type')->select()->toArray();

        return $list;
    }

    /**
     * 先祖列表
     * @param $category
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list($category){
        $list = $this->field('id,type,name,stand,icon_url as icon')->where('category','=',$category)
            ->cache(true)->order('sort desc')->select()->toArray();

        $return = [];
        foreach($list as $k => $v){
            $return[$v['type']][] = array(
                'id' => $v['id'],
                'name' => $v['name'],
                'icon' => $v['icon'],
                'stand' => $v['stand']
            );
        }
        return $return;
    }

    /**
     * 先祖详情
     * @param $ancestor_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($ancestor_id){
        $detail = $this->where('id','=',$ancestor_id)->cache(true)
            ->find()->toArray();
        $icon = (new SkyCategory())->icon();
        $nodes = (new SkyBranch())->detail($ancestor_id);
        $return = array(
            'cover' => $detail['img_url'],
            'name' => $detail['name'],
            'type' => 2,
            'typeAlias' => $detail['category'] == '季节先祖'?$detail['type']:$detail['category'],
            'typeIcon'  => $detail['category'] == '季节先祖'?$icon[$detail['type']]:'',
            'area'      => $detail['category'] == '季节先祖'?"":$detail['type'],
            'position'  => json_decode($detail['position_url'],true),
            'relation'  => array(
                'templateId' => $nodes[0]['templateId'],
                'total' => $nodes[0]['total'],
                'nodes' => $nodes[0]['branch'],
            )
        );

        if(!empty($nodes[1])){
            $return['relation_copy'] = array(
                'templateId' => $nodes[1]['templateId'],
                'total' => $nodes[1]['total'],
                'nodes' => $nodes[1]['branch'],
            );
        }
        return $return;
    }
}